package blackjack.client.appletVisualAndLogic;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import blackjack.client.connection.ClientListener;
import blackjack.client.ConnectionApplet;

/** pass: RW8cg9nb7Yw7
 *  kas.nimi: mihhail.marenkov@gmail.com
 *  https://my-new-blackjack.googlecode.com/svn/tr
 * Connection Panel 
 */
@SuppressWarnings("serial")
public
class ConnectionPanel extends JPanel implements ActionListener, Runnable{
	
	ConnectionApplet client;
	String nimi;
	Thread t;
	String address = "localhost";

	public ConnectionPanel(ConnectionApplet client){
		this.client = client;
		t = new Thread(this);
		init();
	}

	public void init(){
		setBackground(Color.BLACK);
	}

	/**
	 * gets Typed Name, if empty field then message
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(client.getSaatmine().getText().length() > 0 ){
			t.start();
			nimi = client.getSaatmine().getText();
		}else{
			String s="Please write your name.";
			JOptionPane.showMessageDialog(null,s);
		}
	}

	public void run(){
		while(true){
			InetAddress addr = null;
			try {
				client.getCard.setEnabled(true);
				client.enough.setEnabled(true);
				addr = InetAddress.getByName(address);
			} catch (UnknownHostException e1) {
				e1.printStackTrace();
			}
			try {
				client.setSocket(new Socket(addr, 8888)); // JutuServer, PORT
			} catch (IOException e) {
				String st="No connection to the server.";
				JOptionPane.showMessageDialog(null,st);
				System.out.println("No connection to the server");
				return;
			}
			try {
				client.connected();
				client.cp.add(BorderLayout.CENTER, client.mainPanel);
				System.out.println("socket = " + client.getSocket());
				try {
					client.setin(new BufferedReader(new InputStreamReader(client.getSocket()
							.getInputStream())));

				} catch (IOException e) {
					e.printStackTrace();
				}

				try {
					client.setout(new PrintWriter(new BufferedWriter(
							new OutputStreamWriter(client.getSocket().getOutputStream())), true));
					client.setName(nimi);
					client.getout().println(nimi);
				} catch (IOException e) {
					e.printStackTrace();
				}
				BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

				ClientListener l = new ClientListener(client);
				l.start();

				String s = null;

				do {
					try {
						s = in.readLine();
					} catch (IOException e) {
						e.printStackTrace();
					} // blocked...
					if (s.length() > 0) client.getout().println(s); // send message toServer
				}
				while (!s.equals("END")); // END do 
			} 
			finally {
				System.out.println("close...");
				try {
					client.getSocket().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
}